import path from 'node:path'
import process from 'node:process'
import { loadEnv } from 'vite'
import type { ConfigEnv, UserConfig } from 'vite'
import autoprefixer from 'autoprefixer'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VueRouterAutoImports } from 'unplugin-vue-router'
import VueRouter from 'unplugin-vue-router/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import tailwindcss from '@tailwindcss/postcss'

export default ({ mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)

  return {
    base: env.VITE_APP_PUBLIC_PATH,
    plugins: [
      // https://github.com/posva/unplugin-vue-router
      VueRouter({
        extensions: ['.vue'],
        routesFolder: 'src/pages',
        exclude: ['src/pages/**/components/**', 'src/pages/**/composables/**'],
        dts: 'src/types/typed-router.d.ts',
      }),
      // @ts-ignore
      vue(),
      vueJsx(),
      // https://github.com/antfu/unplugin-vue-components
      Components({
        extensions: ['vue'],
        resolvers: [AntDesignVueResolver({
          importStyle: 'less', // 使用 less 样式
        })],
        include: [/\.vue$/, /\.vue\?vue/],
        dts: 'src/types/components.d.ts',
        dirs: ['src/components', 'src/pages/**/components'],
      }),
      // https://github.com/antfu/unplugin-auto-import
      AutoImport({
        include: [
          /\.[tj]sx?$/,
          /\.vue$/,
          /\.vue\?vue/,
        ],
        imports: [
          'vue',
          'pinia',
          '@vueuse/core',
          VueRouterAutoImports,
          {
            'vue-router/auto': ['useLink'],
          },
          {
            'vue-request': ['usePagination', 'useLoadMore', 'useRequest']
          }
        ],
        dts: 'src/types/auto-imports.d.ts',
        dirs: [
          'src/composables',
          'src/utils',
          'src/constants',
        ],
        resolvers: [AntDesignVueResolver()],
      }),
    ],

    server: {
      host: true,
      port: 5297,
      ...env.VITE_APP_API_FORCE_URL ? {
        proxy: {
          [env.VITE_APP_API_FORCE_URL]: {
            target: 'https://cszhixinapi.91xiaojia.com',
            changeOrigin: true,
            rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_API_FORCE_URL}`), '')
          }
        }
      } : null
    },

    resolve: {
      alias: {
        '@': path.join(__dirname, './src'),
      },
    },

    css: {
      postcss: {
        plugins: [
          autoprefixer(),
          tailwindcss(),
        ],
      },
    },

    build: {
      cssCodeSplit: false,
      chunkSizeWarningLimit: 2048,
    },

    optimizeDeps: {
      include: [
        "resize-detector",
        "dayjs/locale/zh-cn"
      ]
    },
    define: {
      __IMAGE__: JSON.stringify("https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"),
      __DEFAULT_AVATAR__: JSON.stringify("/images/default-avatar.png"),
      __USER_LICENSE__: JSON.stringify("https://www.superhrai.com/user-license"),
      __PRIVATE_POLICY__: JSON.stringify("https://www.superhrai.com/private-policy")
    }
  }
}

